"""
数据血缘相关模式
"""

from pydantic import BaseModel
from typing import Optional, List, Dict, Any
from datetime import datetime


class LineageNodeSchema(BaseModel):
    """血缘节点模式"""
    id: str
    type: str  # table, view, temp
    database: Optional[str] = None
    table: str
    full_name: str
    columns: Optional[List[str]] = None
    properties: Optional[Dict[str, Any]] = None
    position: Optional[Dict[str, int]] = None
    
    class Config:
        from_attributes = True


class LineageEdgeSchema(BaseModel):
    """血缘边模式"""
    id: str
    source: str
    target: str
    type: str = "data_flow"
    column_mapping: Optional[Dict[str, str]] = None
    properties: Optional[Dict[str, Any]] = None
    
    class Config:
        from_attributes = True


class DataLineageSchema(BaseModel):
    """数据血缘模式"""
    id: Optional[int] = None
    task_id: int
    task_name: str
    nodes: List[LineageNodeSchema] = []
    edges: List[LineageEdgeSchema] = []
    node_count: int = 0
    edge_count: int = 0
    status: str = "active"
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None
    
    class Config:
        from_attributes = True


class LineageAnalysisRequest(BaseModel):
    """血缘分析请求模式"""
    task_id: int
    force_refresh: bool = False


class LineageAnalysisResponse(BaseModel):
    """血缘分析响应模式"""
    task_id: int
    task_name: str
    analysis_success: bool
    lineage: Optional[DataLineageSchema] = None
    error_message: Optional[str] = None
    processing_time: float = 0.0


class LineageGraphData(BaseModel):
    """血缘图数据模式"""
    nodes: List[Dict[str, Any]] = []
    edges: List[Dict[str, Any]] = []
    layout: str = "dagre"
    
    
class LineageStatistics(BaseModel):
    """血缘统计信息"""
    total_tasks: int = 0
    analyzed_tasks: int = 0
    total_nodes: int = 0
    total_edges: int = 0
    table_count: int = 0
    view_count: int = 0
    temp_table_count: int = 0


class DatabaseConnectionTest(BaseModel):
    """数据库连接测试结果"""
    success: bool
    message: str
    connection_time: float
    database_info: Optional[dict] = None
    error_details: Optional[str] = None


class BatchAnalysisRequest(BaseModel):
    """批量分析请求"""
    force_refresh: bool = False


class BatchAnalysisResponse(BaseModel):
    """批量分析响应"""
    total_tasks: int
    success_count: int
    failed_count: int
    processing_time: float
    results: List[dict] = []
